import type { FC } from 'react'
import React from 'react'
import { useTranslation } from 'react-i18next'
import useConfig from './use-config'
import MethodInput from './components/method-input'
import KeyValue from './components/key-value'
import EditBody from './components/edit-body'
import AuthorizationModal from './components/authorization'
import type { MetaOperatorNodeType } from './types'
import Timeout from './components/timeout'
import CurlPanel from './components/curl-panel'
import cn from '@/utils/classnames'
import Field from '@/components/workflow/nodes/_base/components/field'
import Split from '@/components/workflow/nodes/_base/components/split'
import OutputVars, { VarItem } from '@/components/workflow/nodes/_base/components/output-vars'
import { Settings01 } from '@/components/base/icons/src/vender/line/general'
import { FileArrow01 } from '@/components/base/icons/src/vender/line/files'
import type { NodePanelProps } from '@/components/workflow/types'
import BeforeRunForm from '@/components/workflow/nodes/_base/components/before-run-form'
import ResultPanel from '@/components/workflow/run/result-panel'
import OutputVarList from '@/components/workflow/nodes/_base/components/variable/output-var-list'
import AddButton from '@/components/base/button/add-button'

const i18nPrefix = 'workflow.nodes.metaOperator'

const Panel: FC<NodePanelProps<MetaOperatorNodeType>> = ({
  id,
  data,
}) => {
  const { t } = useTranslation()

  const {
    readOnly,
    isDataReady,
    inputs,
    outputKeyOrders,
    handleMethodChange,
    handleMetaClassTypeChange,
    handleIdentifierChange,
    setTimeout,
    handleVarsChange,
    handleRemoveVariable,
    handleAddOutputVariable,
    // single run
    isShowSingleRun,
    hideSingleRun,
    runningStatus,
    handleRun,
    handleStop,
    inputVarValues,
    setInputVarValues,
    runResult,
  } = useConfig(id, data)

  // To prevent prompt editor in body not update data.
  if (!isDataReady)
    return <>数据未准备，没有找到默认的节点数据配置</>

  return (
    <div className='pt-2'>
      <div className='px-4 pb-4 space-y-4'>
        <Field
          title={t(`${i18nPrefix}.api`)}
        >
          <MethodInput
            nodeId={id}
            readonly={readOnly}
            method={inputs.method}
            onMethodChange={handleMethodChange}
            classType={inputs.class_type}
            onMetaClassTypeChange={handleMetaClassTypeChange}
            identifier={inputs.identifier}
            onIdentifierChange={handleIdentifierChange}
          />
        </Field>
      </div>
      <Split />
      <Timeout
        nodeId={id}
        readonly={readOnly}
        payload={inputs.timeout}
        onChange={setTimeout}
      />
      <Split />
      <div className='px-4 pt-4 pb-2'>
        <Field
          title={t(`${i18nPrefix}.outputVars`)}
          operations={
            <AddButton onClick={handleAddOutputVariable} />
          }
        >

          <OutputVarList
            readonly={readOnly}
            outputs={inputs.outputs}
            outputKeyOrders={outputKeyOrders}
            onChange={handleVarsChange}
            onRemove={handleRemoveVariable}
          />
        </Field>
      </div>
      {isShowSingleRun && (
        <BeforeRunForm
          nodeName={inputs.title}
          onHide={hideSingleRun}
          forms={[
            {
              inputs: [],
              values: inputVarValues,
              onChange: setInputVarValues,
            },
          ]}
          runningStatus={runningStatus}
          onRun={handleRun}
          onStop={handleStop}
          result={<ResultPanel {...runResult} showSteps={false} />}
        />
      )}
    </div>
  )
}

export default React.memo(Panel)
